package net.xi.dmhome.controller;

import cn.hutool.core.map.MapUtil;
import net.xi.dmhome.Utils.DesAesUtil;
import net.xi.dmhome.Utils.ListtoStringUtil;
import net.xi.dmhome.entity.Patient;
import net.xi.dmhome.entity.User;
import net.xi.dmhome.handler.Result;
import net.xi.dmhome.mapper.PatientMapper;
import net.xi.dmhome.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: jxhu
 * @Description:患者信息控制层
 * @Date Created in 2021-02-07 15:22
 * @Modified By:
 */
@RequestMapping("/patient")
@RestController
public class PatientController {

    @Autowired
    PatientMapper patientMapper;
    @Autowired
    PatientService patientService;

    /* @Description: 患者信息添加,@Param:  */
    @PostMapping("/add")
    public Result add(@Validated @RequestBody(required = false) Patient patient) throws Exception {

            int insert = patientMapper.insert(patient);
            if (insert != 0) {

                System.out.println("添加成功");
                return Result.success(200, "success", insert);
            } else {
                return Result.fail("添加失败");
            }
    }

    /* @Description: 根据id查找患者信息,@Param:  */
    @GetMapping("/findbyid/{uuid}")
    public Result getPatientById(@PathVariable("uuid") String uuid) throws Exception {
        Patient patient = patientMapper.selectByUserUUid(uuid);
        String getsymptom = patientMapper.getsymptom(uuid);
        List<String> list = ListtoStringUtil.stringtoList(getsymptom);
        patient.setSymptom(list);
        return Result.success(patient);

    }
    /* @Description: 编辑患者信息,@Param:  */
    @PutMapping("/edit")
    public Result editPatient(@Validated @RequestBody Patient patient) throws Exception {
            boolean update = patientService.saveOrUpdate(patient);
            if (update !=false) {

                System.out.println("更改成功");
                return Result.success(200, "success", patient);
            } else {
                return Result.fail("更改失败");
            }
    }


    /* @Description: 删除患者,@Param:  */
    @DeleteMapping("/delete/{id}")
    public Result deletePatient(@PathVariable("id") Integer id){
        int i = patientMapper.deleteById(id);
        if (i == 1){
            return Result.success("删除成功");
        }else {
            return Result.fail("删除失败");
        }
    }

}

